package main

/*
	在协程中使用channel，则channel的堵塞会等待其余协程
	当所有协程都结束仍未堵塞状态，则会抛出异常“all goroutines are asleep - deadlock!”
*/

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func producter(ch chan int) {
	for i := 1; i <= 20; i++ {
		ch <- i
		fmt.Printf("Producter【%v】\n", i)
		time.Sleep(time.Millisecond * 10)
	}
	// close(ch)
	wg.Done()
}

func consumer(ch chan int) {
	for i := 1; i <= 20; i++ {
		fmt.Printf("Consumer【%v】\n", <-ch)
		time.Sleep(time.Millisecond * 10)
	}
	wg.Done()
}

func main() {
	var cha1 = make(chan int, 20)
	wg.Add(1)
	go producter(cha1)
	wg.Add(1)
	go consumer(cha1)
	wg.Wait()
}
